<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class ObjectSpace::WeakKeyMap - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="../Object.html">Object</a>
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-5B-5D">#[]</a>
    <li ><a href="#method-i-5B-5D-3D">#[]=</a>
    <li ><a href="#method-i-clear">#clear</a>
    <li ><a href="#method-i-delete">#delete</a>
    <li ><a href="#method-i-getkey">#getkey</a>
    <li ><a href="#method-i-inspect">#inspect</a>
    <li ><a href="#method-i-key-3F">#key?</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-ObjectSpace::WeakKeyMap">
    <h1 id="class-ObjectSpace::WeakKeyMap" class="class">
      class ObjectSpace::WeakKeyMap
    </h1>

    <section class="description">
    
<p>An <a href="WeakKeyMap.html"><code>ObjectSpace::WeakKeyMap</code></a> object holds references to any objects, but objects uses as keys can be garbage collected.</p>

<p>Objects used as values can’t be garbage collected until the key is.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-5B-5D" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          map[key] &rarr; value
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value associated with the given <code>key</code> if found.</p>

<p>If <code>key</code> is not found, returns <code>nil</code>.</p>

                              <div class="method-source-code" id="5B-5D-source">
            <pre>static VALUE
wkmap_aref(VALUE self, VALUE key)
{
    VALUE obj = wkmap_lookup(self, key);
    return obj != Qundef ? obj : Qnil;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-5B-5D-3D" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          map[key] = value &rarr; value
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Associates the given <code>value</code> with the given <code>key</code>; returns <code>value</code>.</p>

<p>The reference to <code>key</code> is weak, so when there is no other reference to <code>key</code> it may be garbage collected.</p>

<p>If the given <code>key</code> exists, replaces its value with the given <code>value</code>; the ordering is not affected</p>

                              <div class="method-source-code" id="5B-5D-3D-source">
            <pre>static VALUE
wkmap_aset(VALUE self, VALUE key, VALUE val)
{
    struct weakkeymap *w;
    TypedData_Get_Struct(self, struct weakkeymap, &amp;weakkeymap_type, w);

    if (!FL_ABLE(key) || SYMBOL_P(key) || RB_BIGNUM_TYPE_P(key) || RB_TYPE_P(key, T_FLOAT)) {
        rb_raise(rb_eArgError, &quot;WeakKeyMap must be garbage collectable&quot;);
        UNREACHABLE_RETURN(Qnil);
    }

    struct wkmap_aset_args args = {
        .new_key = key,
        .new_val = val,
    };

    st_update(w-&gt;table, (st_data_t)&amp;key, wkmap_aset_replace, (st_data_t)&amp;args);

    RB_OBJ_WRITTEN(self, Qundef, key);
    RB_OBJ_WRITTEN(self, Qundef, val);

    return val;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-clear" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          clear &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Removes all map entries; returns <code>self</code>.</p>

                              <div class="method-source-code" id="clear-source">
            <pre>static VALUE
wkmap_clear(VALUE self)
{
    struct weakkeymap *w;
    TypedData_Get_Struct(self, struct weakkeymap, &amp;weakkeymap_type, w);

    st_foreach(w-&gt;table, wkmap_free_table_i, 0);
    st_clear(w-&gt;table);

    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete(key) &rarr; value or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete(key) {|key| ... } &rarr; object
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Deletes the entry for the given <code>key</code> and returns its associated value.</p>

<p>If no block is given and <code>key</code> is found, deletes the entry and returns the associated value:</p>

<pre class="ruby"><span class="ruby-identifier">m</span> = <span class="ruby-constant">ObjectSpace</span><span class="ruby-operator">::</span><span class="ruby-constant">WeakKeyMap</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">m</span>[<span class="ruby-string">&quot;foo&quot;</span>] = <span class="ruby-value">1</span>
<span class="ruby-identifier">m</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&quot;foo&quot;</span>) <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">m</span>[<span class="ruby-string">&quot;foo&quot;</span>] <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>If no block given and <code>key</code> is not found, returns <code>nil</code>.</p>

<p>If a block is given and <code>key</code> is found, ignores the block, deletes the entry, and returns the associated value:</p>

<pre class="ruby"><span class="ruby-identifier">m</span> = <span class="ruby-constant">ObjectSpace</span><span class="ruby-operator">::</span><span class="ruby-constant">WeakKeyMap</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">m</span>[<span class="ruby-string">&quot;foo&quot;</span>] = <span class="ruby-value">2</span>
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&quot;foo&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span> <span class="ruby-identifier">raise</span> <span class="ruby-string">&#39;Will never happen&#39;</span>} <span class="ruby-comment"># =&gt; 2</span>
</pre>

<p>If a block is given and <code>key</code> is not found, calls the block and returns the block’s return value:</p>

<pre class="ruby"><span class="ruby-identifier">m</span> = <span class="ruby-constant">ObjectSpace</span><span class="ruby-operator">::</span><span class="ruby-constant">WeakKeyMap</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&quot;nosuch&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;Key #{key} not found&quot;</span> } <span class="ruby-comment"># =&gt; &quot;Key nosuch not found&quot;</span>
</pre>

                              <div class="method-source-code" id="delete-source">
            <pre>static VALUE
wkmap_delete(VALUE self, VALUE key)
{
    struct weakkeymap *w;
    TypedData_Get_Struct(self, struct weakkeymap, &amp;weakkeymap_type, w);

    VALUE orig_key = key;
    st_data_t orig_key_data = (st_data_t)&amp;orig_key;
    st_data_t orig_val_data;
    if (st_delete(w-&gt;table, &amp;orig_key_data, &amp;orig_val_data)) {
        VALUE orig_val = (VALUE)orig_val_data;

        rb_gc_remove_weak(self, (VALUE *)orig_key_data);

        ruby_sized_xfree((VALUE *)orig_key_data, sizeof(VALUE));

        return orig_val;
    }

    if (rb_block_given_p()) {
        return rb_yield(key);
    }
    else {
        return Qnil;
    }
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-getkey" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getkey(key) &rarr; existing_key or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the existing equal key if it exists, otherwise returns <code>nil</code>.</p>

                              <div class="method-source-code" id="getkey-source">
            <pre>static VALUE
wkmap_getkey(VALUE self, VALUE key)
{
    struct weakkeymap *w;
    TypedData_Get_Struct(self, struct weakkeymap, &amp;weakkeymap_type, w);

    st_data_t orig_key;
    if (!st_get_key(w-&gt;table, (st_data_t)&amp;key, &amp;orig_key)) return Qnil;

    return *(VALUE *)orig_key;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-inspect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inspect &rarr; new_string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a new <a href="../String.html"><code>String</code></a> containing informations about the map:</p>

<pre class="ruby"><span class="ruby-identifier">m</span> = <span class="ruby-constant">ObjectSpace</span><span class="ruby-operator">::</span><span class="ruby-constant">WeakKeyMap</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">m</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">value</span>
<span class="ruby-identifier">m</span>.<span class="ruby-identifier">inspect</span> <span class="ruby-comment"># =&gt; &quot;#&lt;ObjectSpace::WeakKeyMap:0x00000001028dcba8 size=1&gt;&quot;</span>
</pre>

                              <div class="method-source-code" id="inspect-source">
            <pre>static VALUE
wkmap_inspect(VALUE self)
{
    struct weakkeymap *w;
    TypedData_Get_Struct(self, struct weakkeymap, &amp;weakkeymap_type, w);

    st_index_t n = st_table_size(w-&gt;table);

#if SIZEOF_ST_INDEX_T &lt;= SIZEOF_LONG
    const char * format = &quot;#&lt;%&quot;PRIsVALUE&quot;:%p size=%lu&gt;&quot;;
#else
    const char * format = &quot;#&lt;%&quot;PRIsVALUE&quot;:%p size=%llu&gt;&quot;;
#endif

    VALUE str = rb_sprintf(format, rb_class_name(CLASS_OF(self)), (void *)self, n);
    return str;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-key-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          key?(key) &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns <code>true</code> if <code>key</code> is a key in <code>self</code>, otherwise <code>false</code>.</p>

                              <div class="method-source-code" id="key-3F-source">
            <pre>static VALUE
wkmap_has_key(VALUE self, VALUE key)
{
    return RBOOL(wkmap_lookup(self, key) != Qundef);
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

